package com.yifeng.controller;

import com.yifeng.dao.PermissionMapper;
import com.yifeng.po.Permission;
import com.yifeng.service.IPermissionService;
import com.yifeng.util.BlogResult;
import com.yifeng.util.LayuiResult;
import com.yifeng.util.TreeNode;
import com.yifeng.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @Decription :
 * author : yifeng
 * 创建时间 ：2019/1/17 :21:17
 * version : 1.0
 */
@Controller
public class PermssionController {

    @Autowired
    private PermissionMapper mapper;

    @Autowired
    private IPermissionService permissionService;

//    @RequestMapping("/api/per/list")
//    @ResponseBody
//    public BlogResult list()
//    {
//        List<Permission> permissions = mapper.loadAll();
//        return new BlogResult(0,"OK",permissions);
//    }
    @RequestMapping("/api/per/parent")
    @ResponseBody
    public BlogResult parent()
    {
        BlogResult result = permissionService.getParent();
        return result;
    }

    @RequestMapping("/api/per/userList")
    @ResponseBody
    public BlogResult userList(long id)
    {
        List<Permission> allPer = mapper.loadAll();
        List<Permission> userPer = mapper.loadUserPermissionsByLoginUserId(id);
        List<TreeNode> result = new ArrayList();
        for (int i=0;i<allPer.size();i++)
        {
            TreeNode node = new TreeNode();
            node.setId(allPer.get(i).getId());
            node.setParentId(allPer.get(i).getParentId());
            node.setTitle(allPer.get(i).getTitle());
            for(int j=0;j<userPer.size();j++){
                if(allPer.get(i).getId() == userPer.get(j).getId()){
                    node.setCheckArr("1");
                    break;
                }else{
                    node.setCheckArr("0");
                }
            }
            result.add(node);
        }
        return new BlogResult(0,"OK",result);
    }
    @RequestMapping("/api/per/roleList")
    @ResponseBody
    public BlogResult roleList(long id)
    {
        List<Permission> allPer = mapper.loadAll();
        List<Permission> userPer = mapper.loadPermissionsByRoleId(id);
        List<TreeNode> result = new ArrayList();
        for (int i=0;i<allPer.size();i++)
        {
            TreeNode node = new TreeNode();
            node.setId(allPer.get(i).getId());
            node.setParentId(allPer.get(i).getParentId());
            node.setTitle(allPer.get(i).getTitle());
            if(userPer.size() == 0){
                node.setCheckArr("0");
            }else{
                for(int j=0;j<userPer.size();j++){
                    if(allPer.get(i).getId() .equals(userPer.get(j).getId()) ){
                        node.setCheckArr("1");
                        break;
                    }else{
                        node.setCheckArr("0");
                    }
                }
            }
            result.add(node);
        }
        return new BlogResult(0,"OK",result);
    }

    @RequestMapping("/api/per/list")
    @ResponseBody
    public LayuiResult<Permission> getAll(int page,int limit)
    {
        LayuiResult<Permission> result = permissionService.getPageAll(page, limit);
        return result;
    }


    @RequestMapping(value="/api/per/addGroup", method= RequestMethod.POST,consumes = "application/json")
    @ResponseBody
    public BlogResult add(@RequestBody Permission permission)
    {
        permission.setIs_parent(1);
        permissionService.add(permission);
        return new BlogResult();
    }

    @RequestMapping(value="/api/per/addPer", method= RequestMethod.POST,consumes = "application/json")
    @ResponseBody
    public BlogResult addPer(@RequestBody Permission permission)
    {
        permission.setIs_parent(0);
        permissionService.add(permission);
        return new BlogResult();
    }

}
